// shopMallPackage/pages/newDynDetail/newDynDetail.js
import { queryDynDetail, approveDynRequest } from "../../../service/onetoone_service"
import {
  queryNewHouseComment,
  approveComment,
  createComment,
  deleteComment,
} from "../../../service/house_service"
import debounce from "../../../tools/debounce"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dynId: null,
    newsItem: {},
    commentList: [],
    isShowCommentPop: false,
    createCommentData: {
      content: "",
      images: [],
      houseNumber: "",
      parentId: "",
      grandId: "",
    },
    //评论的点击数据，评论成功后，直接强更，不去这个拉取列表
    commentData: {},
    commentType: "main",//main是代表一级评论，sub是代表二级带回复的评论
    //一级评论的index:
    oneLevelIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        dynId: options.id
      })
      this.queryDynDetail();
    }
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  // 获取动态详情:
  queryDynDetail() {
    wx.showLoading();
    queryDynDetail(this.data.dynId).then(res => {
      console.log("JJLog:动态详情是===:" + JSON.stringify(res));
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          newsItem: res.data,
          commentList: res.data.comments || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryDynDetail", err);
    })
  },

  //创建一条新评论
  createNewComment() {
    this.setData({
      isShowCommentPop: true,
      commentType: 'refresh',//不用去本地回填数据，直接这个刷新列表
    })
  },
  //创建二级评论:
  createTwoLevelComment(e) {
    console.log("JJLog:===:" + JSON.stringify(e));
    let item = e.detail.data;
    let type = e.detail.type;
    let index = e.detail.index;
    if (app.globalData.openId == item.openId) {
      wx.showModal({
        title: '提示',
        content: '是否删除该评论',
        success: res => {
          if (res.confirm) {
            this.deleteOneComment(item.id)
          }
        }
      })
      return;
    }
    if (type == "main") {
      this.setData({
        'createCommentData.parentId': item.id,
        "createCommentData.grandId": item.id,
        oneLevelIndex: index,
        commentData: item,
        commentType: type
      })
    } else {
      //回复:

    }
    this.setData({
      isShowCommentPop: true
    })
  },
  //创建三级评论:
  createThreeLevelComment(e) {
    let item = e.detail.data;
    let type = e.detail.type;
    let index = e.detail.index;
    let id = e.detail.id;//grandId始终是一级的评论的id
    if (app.globalData.openId == item.openId) {
      wx.showModal({
        title: '提示',
        content: '是否删除该评论',
        // confirmColor: "#FF0000",
        success: res => {
          if (res.confirm) {
            this.deleteOneComment(item.id)
          }
        }
      })
      return;
    }

    this.setData({
      'createCommentData.parentId': item.id,
      "createCommentData.grandId": id,
      oneLevelIndex: index,
      commentData: item,
      commentType: type
    })
    this.setData({
      isShowCommentPop: true
    })
    console.log("JJLog:现在是:" + JSON.stringify(this.data.createCommentData));
  },




  //删除评论:
  deleteOneComment(id) {
    wx.showLoading();
    deleteComment(id, this.data.dynId).then(res => {
      wx.hideLoading();
      console.log("JJLog:删除评论成功:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          commentList: res.data
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "删除失败",
        icon: "none"
      })
      console.error("JJLog:删除评论失败:" + JSON.stringify(err));
    })
  },

  //发送评论
  commentSend(e) {
    console.log("JJLog:评论信息是:" + JSON.stringify(e));
    let comment = e.detail.comment;
    this.setData({
      'createCommentData.content': comment,
      'createCommentData.dynId': this.data.dynId
    })
    debounce(this.sendCommentRequest);
  },
  sendCommentRequest() {
    wx.showLoading();
    createComment(this.data.createCommentData).then(res => {
      wx.hideLoading();
      console.log("JJLog:添加评论成功:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          isShowCommentPop: false
        })
        if (this.data.commentType == "refresh") {
          this.queryDynDetail();
        } else {
          //回复更新:
          this.updateOneLevelComment(res.data, this.data.commentType);
        }
      }
    }).catch(err => {
      console.error("JJLog:创建评论失败:" + JSON.stringify(err));
      wx.hideLoading();
      wx.showToast({
        title: err || "评论失败",
        icon: "none"
      })
    })
  },

  //一级评论更新:
  updateOneLevelComment(id, type) {
    let arr = this.data.commentList;
    let childArr = arr[this.data.oneLevelIndex].child;
    let data = this.data.commentData;
    let obj = {};
    if (type == "main") {
      obj = {
        id: id,
        parentId: data.id,
        openId: app.globalData.openId,
        nickname: app.globalData.nickName,
        avatar: app.globalData.avatar,
        content: this.data.createCommentData.content,
      }
    } else {
      obj = {
        id: id,
        parentId: null,
        openId: app.globalData.openId,
        nickname: app.globalData.nickName,
        avatar: app.globalData.avatar,
        content: this.data.createCommentData.content,
        pnickname: data.nickname,
        pavatar: data.avatar
      }
    }

    childArr.push(obj);
    let kItem = 'commentList[' + this.data.oneLevelIndex + '].child';
    let commentNumber = this.data.commentList[this.data.oneLevelIndex].commentNumber + 1;
    let item = 'commentList[' + this.data.oneLevelIndex + '].commentNumber';
    this.setData({
      [item]: commentNumber,
    })
    this.setData({
      [kItem]: childArr,
    })
  },


  // 点赞来拉
  approveDynAction(e) {
    console.log("JJLog:点赞来拉===:" + JSON.stringify(e));
    // let index = e.currentTarget.dataset.index;
    let item = this.data.newsItem;
    let dynId = e.detail.id;
    wx.showLoading();
    approveDynRequest(dynId).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '点赞成功',
          icon: "none"
        })
        item.approveNumber = item.approveNumber + 1;
        this.setData({
          newsItem: item,
        })

      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },







  hideCommentPop() {
    this.setData({
      isShowCommentPop: false
    })
  },







})